//
//  ViewController.m
//  09_UITableViewEdit
//
//  Created by 徐昊 on 16/7/26.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"

#define kTableViewName @"reuseIndentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray *array;
// 选中下标的数组
@property (nonatomic, strong) NSMutableArray<NSIndexPath *> *selectIndexArray;
@property (nonatomic, strong) UITableView *nameTableView;
// 选中名字的数组
@property (nonatomic, strong) NSMutableArray<NSString *> *selectedNameArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSArray *arr = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.array = [NSMutableArray arrayWithArray:arr];
    self.selectedNameArray = [NSMutableArray array];
    self.selectIndexArray = [NSMutableArray array];
    self.nameTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _nameTableView.delegate = self;
    _nameTableView.dataSource = self;
    [self.view addSubview:_nameTableView];
    [_nameTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableViewName];
    // tableView编辑
    // 1.设置右barButtonItem 为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
}
// 2.让tableView进入编辑状态
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_nameTableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        // 要删除选中的数组
        // 删除数据源
        //        _array removeObjectsAtIndexes:<#(nonnull NSIndexSet *)#>
        [_array removeObjectsInArray:_selectedNameArray];
        [_nameTableView deleteRowsAtIndexPaths:_selectIndexArray withRowAnimation:UITableViewRowAnimationFade];
        [_selectedNameArray removeAllObjects];
        [_selectIndexArray removeAllObjects];
        NSLog(@"完成");
    }
}
// 3.控制tableView能否进行编辑 NO 不能编辑 YES 可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 移动代码快捷键 command + option + ] 和 [
// 4.返回编辑状态的状态(样式) 默认为delete
// tableView Delegate
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
#pragma mark - 多选
    return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 在全选状态下, 数组中添加对应选中的下标
        [_selectIndexArray addObject:indexPath];
        // 添加到选中名字的数组
        [_selectedNameArray addObject:_array[indexPath.row]];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        [_selectIndexArray removeObject:indexPath];
        [_selectedNameArray removeObject:_array[indexPath.row]];
    }
    
}
#pragma mark - tableView DataSource

// 5.对应按钮的点击方法
// 滑动删除只和这个模块有关系
// tableView Datasource
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
#warning 如果对tableView进行添加/修改数据源, 先修改数据源, 再去进行对应的添加/删除
        // 1.修改数据源
        [_array removeObjectAtIndex:indexPath.row];
        // 2.对应修改cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_array insertObject:@"王鍠" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        NSLog(@"插入");
    }
    // tableView不论是删除还是添加, 都已经进行了刷新操作
}

// 1.可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 2.更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    [_array exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    NSLog(@"%@", _array);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:kTableViewName forIndexPath:indexPath];
    cell.textLabel.text = _array[indexPath.row];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
